package dt.inlee.common.tools;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.Ordered;

import com.opensymphony.xwork2.ActionContext;

import dt.inlee.common.constant.CommonConstant;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.LogInfo;
import dt.inlee.domain.SysUser;
import dt.inlee.system.service.LogInfoService;
import dt.inlee.system.service.SysUserService;
import dt.inlee.system.vo.SysUserLoginBean;

/**
 * 拦截操作日志
 * 
 * @author shiw
 * 
 */
@SuppressWarnings("unused")
public class EventLogInterceptor implements Ordered {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private LogInfoService logInfoService;
	private int order = 1;
	SysUserLoginBean sysUserLoginBean = (SysUserLoginBean) ActionContext
			.getContext().getSession().get(CommonConstant.SESSION_USER);

    private Log logger = LogFactory.getLog(EventLogInterceptor.class);
	@SuppressWarnings({"rawtypes", "serial", "unchecked"})
    public final static Map map = new HashMap() {
		{
			put("key1", "value1");
			put("key2", "value2");
		}
	};

	/**
	 * 拦截操作日志类
	 * 
	 * @param point
	 * @return
	 * @throws Throwable
	 */
	public Object serviceIntercept(JoinPoint point) throws Throwable {
		// System.out.println("into serviceIntercept");
		if (!BaseUtil.isEmpty(sysUserLoginBean)) {
			SysUser createUser = sysUserService
					.findSysUserById(sysUserLoginBean.getId());
			String methodName = null;
			Object[] args = null;
			// 获取操作方法名称
			try {
				methodName = point.getSignature().getName();
				// 获取方法参数
				args = point.getArgs();

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			// 操作日志信息
			String logUserName = createUser.getRealName();
			int wyId = createUser.getWyId();
			if (methodName.startsWith("save")) {
				// //保存操作日志
				this.saveLog(args[0], sysUserLoginBean.getIp(), logUserName,
						"新增", wyId + "");
			} else if (methodName.startsWith("update")) {
				// //保存操作日志
				this.saveLog(args[0], sysUserLoginBean.getIp(), logUserName,
						"更新", wyId + "");
			} else if (methodName.startsWith("delete")) {
				// //保存操作日志
				this.saveLog(args[0], sysUserLoginBean.getIp(), logUserName,
						"删除", wyId + "");
			}
		}
		return null;
	}

	public int getOrder() {
		// TODO Auto-generated method stub
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	/**
	 * 保存日志
	 * 
	 * @param obj
	 * @param logIp
	 * @param logUserName
	 * @param logType
	 */
	public void saveLog(Object obj, String logIp, String logUserName,
			String logType, String enterpriseId) {
		// System.out.println("into saveLog");
		SysUserLoginBean loginUser = (SysUserLoginBean) ActionContext
				.getContext().getSession().get(CommonConstant.SESSION_USER);
		try {
			// 初始化log表
			LogInfo log = new LogInfo();

			if (null != obj) // 业务操作
			{

				// 获取物理表名
				// AbstractEntityPersister classMetadata =
				// (SingleTableEntityPersister) operationService
				// .getMethodName(obj.getClass());
				// if (classMetadata != null) {
				// String logTable = classMetadata.getTableName();
				//
				// }
				log.setObjectname(obj.getClass() + "");// 表名
				log.setDtime(new Date());// 获取操作时间
				log.setOperatorUser(logUserName);// 登陆用户姓名
				log.setOperatorOrg(loginUser.getOrganizationName());
				log.setBelongsCompany(enterpriseId);
				log.setIp(logIp);// ip地址
				log.setOptype(logType);// 操作类型(C:Create;R:
				// Read;U:Update;D:Delete)
				log.setRemark("对" + log.getObjectname() + "执行了"
						+ log.getOptype());
				log.setState("1");
				logInfoService.addLogInfo(log);
				// }

			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
